#include "cshape.h"

// CShape构造负责初始化起点终点和矩形框对角线，画笔粗细、颜色，形状填充
CShape::CShape(QPointF start, QPointF end, int Width, QColor color, QBrush brush_input)
{
    // 初始化矩形的四个顶点
    SetStartandEnd(start,end);
    // 生成其余控制点并初始化paintfordraw
    GenerateControlPoints();

    // 设置框线的样式
    QPen temPen;
    temPen.setColor(color);
    temPen.setWidth(Width);
    this->setPen(temPen);

    // 设置填充样式
    this->setBrush(brush_input);

    //使图形可选中
    this->setFlag(QGraphicsItem::ItemIsSelectable);
    //使图形可移动
    this->setFlag(QGraphicsItem::ItemIsMovable);
}
CShape::CShape(qreal start_x, qreal start_y, qreal end_x, qreal end_y, int Width, QColor color,  QBrush brush_input):
    CShape(QPointF(start_x,start_y), QPointF(end_x,end_y), Width, color, brush_input)
{
}
// SetStartandEnd用于设置起点和终点
void CShape::SetStartandEnd(QPointF start, QPointF end) {
    points[S1] = start;
    points[E1] = end;
    points[S2] = QPointF(start.x(), end.y());
    points[E2] = QPointF(end.x(), start.y());
}
void CShape::SetStartandEnd(qreal start_x, qreal start_y, qreal end_x, qreal end_y) {
    SetStartandEnd( QPointF(start_x, start_y),QPointF(end_x, end_y) );             //更改四个点坐标
}

//GenerateControlPoints用于生成4边中点的控制点
void CShape::GenerateControlPoints() {
    qreal xmax = points[S1].x() > points[E1].x() ? points[S1].x() : points[E1].x(), ymax =points[S1].y() > points[E1].y() ? points[S1].y() : points[E1].y();
    qreal xmin = points[S1].x() < points[E1].x() ? points[S1].x() : points[E1].x(), ymin =points[S1].y() < points[E1].y() ? points[S1].y() : points[E1].y();
    points[ML] = QPointF( xmin, ( ymax + ymin ) / 2);
    points[MR] = QPointF( xmax, ( ymax + ymin ) / 2);
    points[MU] = QPointF( ( xmax + xmin ) / 2, ymax);
    points[MD] = QPointF( ( xmax + xmin ) / 2, ymin);
    // 生成8个控制点显示图形
    QPointF Delta(-POINT_SIZE.width() / 2, -POINT_SIZE.height() / 2);
    for (int i = 0; i < 8; i++) PointsforDraw[i] = QRectF(points[i] + Delta, POINT_SIZE);   // 设置8个正方形框的范围
}

// 移动当前选定的控制点
void CShape::MoveControlPoint(QPointF CurrentPos) {
    int theOnePoint = WitchPointsisSelected, theOtherPoint;
    // 拖动四边中点改变图形形状时，控制图形不重合，并将其转换为拖动四顶点
    switch (WitchPointsisSelected) {
    case MU:
        if(CurrentPos.y() < points[MD].y() + 10){
               return;
        }
        else {
               theOnePoint = points[S1].y() > points[E1].y() ? S1 : E1;
               CurrentPos.rx() = points[theOnePoint].x();
        }
        break;
    case MD:
        if(CurrentPos.y() > points[MU].y() - 10){
            return;
        }
        else {
            theOnePoint = points[S1].y() < points[E1].y() ? S1 : E1;
            CurrentPos.rx() = points[theOnePoint].x();
        }
        break;
    case MR:
        if(CurrentPos.x() < points[ML].x() + 10){
            return;
        }
        else {
            theOnePoint = points[S1].x() > points[E1].x() ? S1 : E1;
            CurrentPos.ry() = points[theOnePoint].y();
        }
        break;
    case ML:
        if(CurrentPos.x() > points[MR].x() - 10){
            return;
        }
        else {
            theOnePoint = points[S1].x() < points[E1].x() ? S1 : E1;
            CurrentPos.ry() = points[theOnePoint].y();
        }
        break;
    default:
        break;
    }
    //找对角线上另一顶点
    theOtherPoint = theOnePoint < 2 ? theOnePoint + 2 : theOnePoint - 2;

    //更新四个点的位置
    if(theOnePoint == S1 || theOnePoint == E1) {
        points[theOnePoint] = CurrentPos;
        points[S2] = QPointF(points[theOnePoint].x(), points[theOtherPoint].y());
        points[E2] = QPointF(points[theOtherPoint].x(), points[theOnePoint].y());
    }
    else if(theOnePoint == S2 || theOnePoint == E2){
        points[theOnePoint] = CurrentPos;
        points[S1] = QPointF(points[theOnePoint].x(), points[theOtherPoint].y());
        points[E1] = QPointF(points[theOtherPoint].x(), points[theOnePoint].y());
    }
    else return;

    GenerateControlPoints();             //通过四个顶点位置的更新,更新其他四个中心点;
}

// 绘出边界框和控制点
void CShape::PaintBoundingandControlPoints(QPainter *painter,QRectF boundingRect) {
    QPen pen;
    pen.setColor("black");
    pen.setWidth(1);
    painter->setPen(pen);
    painter->setBrush(Qt::NoBrush);
    //如果为选中的控制点则将他的边框填充为红色
    for (int i = 0;i < 8; i++ ) {
        if(i != WitchPointsisSelected) painter->drawRect(PointsforDraw[i]);
        else {
            painter->setBrush(Qt::red);
            painter->drawRect(PointsforDraw[i]);
            painter->setBrush(Qt::NoBrush);
        }
    }
    pen.setStyle(Qt::DashLine);          //将边框线改为虚线
    painter->setPen(pen);
    painter->drawRect(boundingRect);        //绘制出边框
    prepareGeometryChange();
    update();
}

// 支持旋转，传入当前坐标与框中心坐标在场景中的坐标，计算出BeginScenePos和CurrentScenePos与item中心的夹角，以此旋转item
void CShape::SupportRotate(QPointF CurrentScenePos, QPointF CenterScenePos) {
    QPointF Delta1 = BeginScenePos - CenterScenePos, Delta2 = CurrentScenePos - CenterScenePos;
    qreal angle11 = qAtan2(Delta1.y(), Delta1.x()), angle12 = qAtan2(Delta2.y(), Delta2.x());
    setTransformOriginPoint(boundingRect().center());
    setRotation(BeginTransform.TxRotate + qRadiansToDegrees(angle12-angle11));
}


